package net.coding.api.test.common;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.KeyPair;
import com.jcraft.jsch.Session;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.util.FS;

/**
 * Created with IntelliJ IDEA.
 * User: chenfeilong
 * Date: 2018/1/26
 * Time: 14:10
 * Description:
 */
public class CustomJschConfigSessionFactory extends JschConfigSessionFactory {
    private JSch jsch;
    private KeyPair keyPair;

    public CustomJschConfigSessionFactory(KeyPair keyPair, JSch jsch) {
        this.keyPair = keyPair;
        this.jsch = jsch;
    }

    @Override
    protected void configure(OpenSshConfig.Host host, Session session) {
        session.setConfig("StrictHostKeyChecking", "no");
    }

    @Override
    protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {

        jsch.removeAllIdentity();
        jsch.addIdentity(new SshKeyIdentity(keyPair), null);
        return jsch;
    }
}
